package com.xindacloud.sms.portal.handler;

import com.google.gson.Gson;
import com.xindacloud.sms.portal.exception.RestTemplateException;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.stream.Collectors;

public class SmsRestTemplateErrorHandler extends DefaultResponseErrorHandler {

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        if (response.getStatusCode().is4xxClientError() || response.getStatusCode().is5xxServerError()) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getBody()))) {
                String httpBodyResponse = reader.lines().collect(Collectors.joining(""));
                ErrorResponse errorResponse = new Gson().fromJson(httpBodyResponse, ErrorResponse.class);
                throw new RestTemplateException(errorResponse.getPath(), response.getStatusCode(), errorResponse.getMessage());
            }
        }
    }

}
